-- 简单的异步事件库，所有操作都先压入队列，然后在runloop中统一处理
-- 以此来模拟异步的效果
local cmdQueue = {}
local lib = {}

-- 从流中读取一行，读取成功后触发callback事件
function lib.readline(stream, callback)
	local nextCmd = function ()
		callback(stream:read())
	end
	table.insert(cmdQueue, nextCmd)
end

-- 向流写一行,写完后触发callback事件
function lib.writeline(stream, line, callback)
	local nextCmd = function() 
		callback(stream:write(line))
	end
	table.insert(cmdQueue, nextCmd)
end

-- 停止
function lib.stop()
	table.insert(cmdQueue, "stop")
end

function lib.runloop()
	while true do 
		local nextCmd = table.remove(cmdQueue, 1)
		if nextCmd == "stop" then
			break
		else
			nextCmd()
		end
	end
end

return lib
